
package de.fhhof.brst.event;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import de.fhhof.brst.dao.EventDAO;
import de.fhhof.brst.dao.HibernateEventDAO;
import de.fhhof.brst.data.Booking;
import de.fhhof.brst.data.Event;
import de.fhhof.brst.data.User;
import de.fhhof.brst.session.SessionBean;
@ManagedBean
@RequestScoped
public class BookEvent implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID =  -8051777726573275527L;

  @ManagedProperty(value = "#{sessionBean}")
  private de.fhhof.brst.session.SessionBean sessionBean;

  private de.fhhof.brst.dao.EventDAO eventDAO =  HibernateEventDAO.getHibernateEventDAO();

  private de.fhhof.brst.data.Event currentEvent;

  private de.fhhof.brst.data.User currentUser;

  private int bookingCount;

  public BookEvent() {
  }

  @PostConstruct
  public void init() {
		currentEvent = sessionBean.getSelectedEvent();
		currentUser = sessionBean.getUser();
  }

  public String submit() {
		Booking booking = new Booking();
		booking.setEvent_id(currentEvent.getId());
		booking.setUser_id(currentUser.getId());
		
		for(int i = 0; i < bookingCount; i++) {
			eventDAO.bookEvent(booking);
		}
		
		System.out.println("Buchung erfolgreich");
		
		FacesMessage message = new FacesMessage("Buchung erfolgreich!");
        FacesContext.getCurrentInstance().addMessage(null, message);
        
        return "/user_bookings_frame.xhtml?faces-redirect=true";
  }

  public void setSessionBean(de.fhhof.brst.session.SessionBean sessionBean) {
		this.sessionBean = sessionBean;
  }

  public de.fhhof.brst.session.SessionBean getSessionBean() {
		return sessionBean;
  }

  public int getBookingCount() {
		return bookingCount;
  }

  public void setBookingCount(int bookingCount) {
		this.bookingCount = bookingCount;
  }

}
